﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cs499.Database;

namespace cs499.Locations
{
    public partial class ListLocations : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["action"] != null)
            {
                if (Request.QueryString["action"] == "success")
                {
                    ListActionMessage.Text = "Location Successfully added - ";
                }
            }
            else
            {
                ListActionMessage.Text = "";
            }
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton locationsMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("LocationsMenuButton");
            locationsMenuButton.CssClass = "selected";

            if (HttpContext.Current.Session["CurrentDepartmentId"] != null)
            {
                int CurrentDepartmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                cs499.Database.Department CurrentDepartment = cs499.Database.ListOfDepartment.GetDepartment(ID: CurrentDepartmentId);
                //EntityDataSource1.Where = "it.DepartmentId=" + (CurrentDepartment.Id).ToString();
                DepartmentLabel.Text = CurrentDepartment.Name;

            }
            else
            {
                DepartmentLabel.Text = "(No departments defined)";
            }
        }

        protected void exportBuildingButton_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder exportFile = new System.Text.StringBuilder();

            //add header info
            exportFile.AppendLine("#FIELD INFO");
            exportFile.AppendLine("#Code: two letter code to represent the building (eg TH for Technology Hall)");
            exportFile.AppendLine("#Name: then name of the building (eg Technology Hall)");
            exportFile.AppendLine("");

            //add example lines
            exportFile.AppendLine("#EXAMPLES");
            exportFile.AppendLine("#TH,Technology Hall");
            exportFile.AppendLine("#EB,Engineering Building");
            exportFile.AppendLine("#SC,Shelby Center");
            exportFile.AppendLine("");
            exportFile.AppendLine("");

            //add field name line
            exportFile.AppendLine("#Code,Name");

            Model1Container database = new Model1Container();
            foreach (Building building in database.Set<Building>().Where(deptofilter => deptofilter.Department.Name == Master.DepartmentSelection.SelectedValue ))
            {
                exportFile.AppendLine(building.Code + "," +
                                      building.Name);
            }

            Response.ContentType = @"application/x-msdownload";
            Response.AppendHeader("content-disposition", "attachment; filename=Buildings.csv");

            Response.Write(exportFile.ToString());
            Response.Flush();
            Response.End();
        }

        protected void exportRoomButton_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder exportFile = new System.Text.StringBuilder();

            //add header info
            exportFile.AppendLine("#FIELD INFO");
            exportFile.AppendLine("#Number: the number of the room (eg N324)");
            exportFile.AppendLine("#Capacity: the maximum number of people allowed in the room (eg 20)");
            exportFile.AppendLine("#Building Code: two letter code to represent the building the room is in (eg TH for Technology Hall)");
            exportFile.AppendLine("");

            //add example lines
            exportFile.AppendLine("#EXAMPLES");
            exportFile.AppendLine("#N308,20,TH");
            exportFile.AppendLine("#N324,30,TH");
            exportFile.AppendLine("#208,30,SC");
            exportFile.AppendLine("");
            exportFile.AppendLine("");

            //add field name line
            exportFile.AppendLine("#Number,Capacity,Building Code");

            Model1Container database = new Model1Container();
            foreach (Room room in database.Set<Room>().Where(deptofilter => deptofilter.Department.Name == Master.DepartmentSelection.SelectedValue))
            {
                Building building = database.Set<Building>().FirstOrDefault(b => (b.Id == room.BuildingID));
                exportFile.AppendLine(room.Number + "," +
                                      room.Capacity.ToString() + "," +
                                      building.Code);
            }

            Response.ContentType = @"application/x-msdownload";
            Response.AppendHeader("content-disposition", "attachment; filename=Rooms.csv");

            Response.Write(exportFile.ToString());
            Response.Flush();
            Response.End();
        }
    }
}